class MyException extends Exception { --------- }
class Circle { private int r; public void setRadius(int n) { r=n; } public void area() { double a=3.14 * r * r; System.out.println("Area is "+a); } } class demo { public static void main(String args[]) { Circle a = new Circle(); a.setRadius(5); a.area(); System.out.println("Program End"); } }
Area is 78.5 Program End
class InvalidRadiusException extends Exception { public InvalidRadiusException(String errmsg) { super(errmsg); } } class Circle { private int r; public void setRadius(int n) { if(n<0) throw new InvalidRadiusException("Invalid Radius :"+n); r=n; } public void area() { double a=3.14 * r * r; System.out.println("Area is "+a); } } class demo { public static void main(String args[]) { try { Circle a = new Circle(); a.setRadius(-5); a.area(); } catch(InvalidRadiusException er) { System.out.println("Error : "+er); } System.out.println("Program End"); } }
Error : Invalid Radius -5 Program End